package controllers;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Set;

import models.Advert;
import models.Employment;
import play.mvc.Controller;
import play.mvc.With;
import service.AdpoService;
import service.EmploymentService;

@With(Secure.class)
public class EmploymentAction extends Controller{
	
	/**
	 * 查询全部招聘信息的方法
	 */
	@Check("InformationManage")
	public static void list(){
		List<Employment> employments = EmploymentService.findAll();
		render(employments);
	}
	
	
	/**
	 * 跳转向招聘信息的添加页面
	 */
	@Check("InformationManage")
	public static void blank(){
		render();
	}
	
	
	/**
	 * 招聘信息的添加方法
	 * @param employment  招聘信息对象
	 */
	@Check("InformationManage")
	public static void create(Employment employment){
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			employment.empDate = sf.parse(sf.format(Calendar.getInstance().getTime()));
			System.out.println(employment.empDate);
			EmploymentService.add(employment);
			flash.success("添加成功");
			list();
		} catch (Exception e) {
			System.out.println("类EmploymentAction中的create（）方法抛异常");
			e.printStackTrace();
			flash.success("添加失败");
			blank();
		}

	}
	
	
	/**
	 * 跳转向修改页面
	 * @param id   招聘信息id
	 */
	@Check("InformationManage")
	public static void show(Long id){
		Employment employment = EmploymentService.findById(id);
		render(employment);
	}
	
	/**
	 * 修改招聘信息的方法
	 * 
	 * @param employment 一个招聘信息对象
	 */
	@Check("InformationManage")
	public static void update(Employment employment){
		//String detail;
		try {
			EmploymentService.update(employment);
			flash.success("修改成功");
			list();
		} catch (Exception e) {
			System.out.println("类employmentAction中的update（）方法中抛异常");
			e.printStackTrace();
			flash.success("修改失败");
			show(employment.id);
		}
	}
	
	@Check("InformationManage")
	public static void delete(Long id){
		try {
			EmploymentService.delete(id);
			flash.success("删除成功");
			list();
		} catch (Exception e) {
			System.out.println("类EmploymentAction中的delete（）方法抛异常");
			e.printStackTrace();
            flash.success("删除失败");
            list();
		}
	}
}
